package be.kuleuven.mume.beans;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import be.kuleuven.mume.PMF;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class ProfBean {
	
	@NotPersistent
	private static final PersistenceManager pm = PMF.get().getPersistenceManager();
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key profId;
	@Persistent
	private String name;
	@Persistent
	private String mail;
	@Persistent
	private String twitterName;
	
	public String getId(){
		return KeyFactory.keyToString(this.profId);
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}
	public String getMail() {
		return mail;
	}

	public void setTwitterName(String twitterName) {
		this.twitterName = twitterName;
	}
	public String getTwitterName() {
		return twitterName;
	}
	
	public void store(){
		pm.makePersistent(this);
		pm.flush();
	}

	public static class ProfBeanFactory{
		public static ProfBean getProfById(String profId){
			return pm.getObjectById(ProfBean.class, KeyFactory.stringToKey(profId));
		}
		
		@SuppressWarnings("unchecked")
		public static List<ProfBean> getProffen(){
			Query query = pm.newQuery(ProfBean.class);
			return (List<ProfBean>)query.execute();
		}
	}
}
